package com.wengu.chess;


import org.junit.jupiter.api.Test;

/**
 * @Author 王海雷
 * @Date 2021/6/3 19:10
 * @Version 1.0
 */
public class ArithmeticChess {
    @Test
    public void testChess(){
        int length = 11;
        int[][] arrChess1 = new int[length][length];
        System.out.println("=================原始数组===============");
        arrChess1[5][5] = 1;
        arrChess1[5][7] = 2;
        arrChess1[6][6] = 1;
        int sum = 0;
        for (int[] arr : arrChess1) {
            for (int i : arr) {
                System.out.printf("%3d",i);
                if (i != 0){
                    sum++;
                }
            }
            System.out.println();
        }
        System.out.println("=================稀疏数组===============");
        int[][] sparse = new int[sum + 1][3];
        sparse[0][0] = length;
        sparse[0][1] = length;
        sparse[0][2] = sum;
        int count = 0;
        for (int i = 0; i < arrChess1.length; i++) {
            for (int j = 0; j < arrChess1[i].length; j++) {
                if (arrChess1[i][j] != 0){
                    count++;
                    sparse[count][0] = i;
                    sparse[count][1] = j;
                    sparse[count][2] = arrChess1[i][j];
                }
            }
        }
        for (int[] arr : sparse) {
            for (int i : arr) {
                System.out.printf("%3d",i);
            }
            System.out.println();
        }
        System.out.println("=================稀疏数组转换原始数组===============");
        int[][] arrChess2 = new int[sparse[0][0]][sparse[0][1]];
        for (int i = 1; i < sparse.length; i++) {
            arrChess2[sparse[i][0]][sparse[i][1]] = sparse[i][2];
        }
        for (int[] arr : arrChess2) {
            for (int i : arr) {
                System.out.printf("%3d",i);
            }
            System.out.println();
        }

    }
}
